Completed
Push — master ( 3f67a9...8b7e63 )
by Andres
28s
created

elements.js ➔ ... ➔ ct.getChance   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
dl 0
loc 7
rs 9.4285
nop 1
1
/**
2
 elements
3
 Component that handles the periodic table tab.
4
 It includes the logic to purchase and display elements.
5
6
 @namespace Components
7
 */
8
'use strict';
9
10
angular.module('game').component('elements', {
11
  templateUrl: 'views/elements.html',
12
  controller: ['$timeout', 'state', 'data', elements],
13
  controllerAs: 'ct'
14
});
15
16
function elements($timeout, state, data) {
17
  let ct = this;
18
  ct.elementPrice = 1;
19
  ct.state = state;
20
  ct.data = data;
21
  ct.outcome = {};
22
23
  ct.getChance = function(element) {
24
    let bonus = 0;
25
    for(let isotope in data.elements[element].isotopes){
26
      bonus += state.player.resources[isotope].number*data.constants.ELEMENT_CHANCE_BONUS;
27
    }
28
    return Math.min(1, data.elements[element].abundance*(1+bonus));
29
  };
30
31
  ct.buyElement = function (element) {
32
    if (state.player.elements[element].unlocked) {
33
      return;
34
    }
35
    if (state.player.resources.dark_matter.number >= ct.elementPrice) {
36
      state.player.resources.dark_matter.number -= ct.elementPrice;
37
38
      if(Math.random() < ct.getChance(element)){
39
        state.player.elements[element].unlocked = true;
40
        state.player.elements[element].generators['1'] = 1;
41
        state.player.elements_unlocked++;
42
        ct.outcome[element] = 'Success';
43
      }else{
44
        ct.outcome[element] = 'Fail';
45
      }
46
      $timeout(function(){ct.clearMessage(element);}, 1000)
47
    }
48
  };
49
50
  ct.clearMessage = function (element) {
51
    ct.outcome[element] = '';
52
  };
53
54
  /* This function returns the class that determines on which
55
  colour an element card */
56
  ct.elementClass = function (element) {
57
    if(!state.player.elements[element]){
58
      return 'element_unavailable';
59
    }
60
    if (state.player.elements[element].unlocked) {
61
      return 'element_purchased';
62
    }else{
63
      if(state.player.resources.dark_matter.number >= ct.elementPrice) {
64
        return 'element_cost_met';
65
      }else{
66
        return 'element_cost_not_met';
67
      }
68
    }
69
  };
70
71
  /* This function returns the class that determines the secondary
72
  colour of an element card */
73
  ct.elementSecondaryClass = function (element) {
74
    return ct.elementClass(element) + '_dark';
75
  };
76
}
77